package com.example.pixelyear.utils.dialog

import android.graphics.Color
import com.example.pixelyear.R
import com.example.pixelyear.ui.base.BaseActivity
import com.example.pixelyear.utils.TipsDialogUtils
import com.example.pixelyear.utils.UIUtils
import com.example.pixelyear.utils.Utils
import com.example.pixelyear.utils.extension.setOnDoubleClickListener
import com.example.pixelyear.utils.extension.stringText
import com.socks.library.KLog
import kotlinx.android.synthetic.main.dialog_choose_color_for_egg.view.*
import org.adw.library.widgets.discreteseekbar.DiscreteSeekBar

/**
 * 彩蛋高级颜色编辑
 */
class ChooseColorForEggDialog(val context: BaseActivity) : BaseChooseColorDialog(context) {
    private val trans = 0
    private val red = 1
    private val greed = 2
    private val blue = 3
    private var transColor = 255
    private var redColor = 0
    private var greenColor = 0
    private var blueColor = 0
    private var color = 0

    override fun getLayoutID(): Int {
        return R.layout.dialog_choose_color_for_egg
    }

    override fun initAny() {
        rootView.Dialog_Egg_Tips.setOnDoubleClickListener {
            TipsDialogUtils.showSureDialog(context, content = UIUtils.getString(R.string.color_msg)) {}
        }

        rootView.Dialog_Egg_Trans.setOnProgressChangeListener(MyListener(trans))
        rootView.Dialog_Egg_Red.setOnProgressChangeListener(MyListener(red))
        rootView.Dialog_Egg_Green.setOnProgressChangeListener(MyListener(greed))
        rootView.Dialog_Egg_Blue.setOnProgressChangeListener(MyListener(blue))

        rootView.Dialog_Egg_Sure.setOnDoubleClickListener {
            if (rootView.Dialog_Egg_Text.stringText().isNotEmpty())
                if (color == -1) {
                    Utils.showToast("颜色本来就是白色哦～")
                } else {
                    click?.click(
                        color,
                        rootView.Dialog_Egg_Text.stringText()
                    )
                }
            else Utils.showToast("颜色心语不能为空哦～")
        }
    }

    override fun show(color: Int, text: String) {
        super.show()
        rootView.Dialog_Egg_Text.setTextColor(color)
        rootView.Dialog_Egg_Text.setText(text)
        val alpha = color.ushr(24)
        val red = color and 0xff0000 shr 16
        val green = color and 0x00ff00 shr 8
        val blue = color and 0x0000ff
        rootView.Dialog_Egg_Trans.progress = alpha
        rootView.Dialog_Egg_Red.progress = red
        rootView.Dialog_Egg_Green.progress = green
        rootView.Dialog_Egg_Blue.progress = blue
    }


    inner class MyListener(private val type: Int) : DiscreteSeekBar.OnProgressChangeListener {
        override fun onProgressChanged(seekBar: DiscreteSeekBar?, value: Int, fromUser: Boolean) {
            when (type) {
                trans -> {
                    transColor = value
                }
                red -> {
                    redColor = value
                }
                greed -> {
                    greenColor = value
                }
                blue -> {
                    blueColor = value
                }
            }
            color = Color.argb(transColor, redColor, greenColor, blueColor)
            KLog.e("yxs", "颜色值：透明：$transColor 红：$redColor 绿：$greenColor 蓝：$blueColor  颜色：$color")
            rootView.Dialog_Egg_Text.setTextColor(color)
        }

        override fun onStartTrackingTouch(seekBar: DiscreteSeekBar?) {
        }

        override fun onStopTrackingTouch(seekBar: DiscreteSeekBar?) {
        }

    }

}
